/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.doeacc.tms.web.bean;

import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import edu.doeacc.tms.domain.Shipment;
import edu.doeacc.tms.domain.ShippingInvoice;
import edu.doeacc.tms.service.dao.ShipmentFacade;
import edu.doeacc.tms.service.dao.ShippingInvoiceFacade;
import edu.doeacc.tms.web.util.JsfUtil;

/**
 *
 * @author slok
 */
@ManagedBean(name = "shippingBean")
@ViewScoped
public class ShippingBean {

    @EJB
    private ShipmentFacade shipmentFacade;
    @EJB
    private ShippingInvoiceFacade invoiceFacade;
    @ManagedProperty("#{msgs}")
    private transient ResourceBundle bundle;
    private Shipment current;
    private Long invoiceNumber;

    public void setBundle(ResourceBundle bundle) {
        this.bundle = bundle;
    }

    private ShipmentFacade getFacade() {
        return shipmentFacade;
    }

    public Shipment getSelected() {
        if (current == null) {
            if (this.currentId > -1) {
                this.current = getFacade().find(this.currentId);
            } else {
                current = new Shipment();
            }
        }
        return current;
    }
    Long currentId = -1L;

    public Long getCurrentId() {
        return getSelected().getId();
    }

    public void setCurrentId(Long id) {
        this.currentId = id;
    }

    public Long getInvoiceNumber() {
        return invoiceNumber;
    }

    public void setInvoiceNumber(Long invoiceNumber) {
        this.invoiceNumber = invoiceNumber;
    }

    public void addInvoice() {
        if (invoiceNumber != null) {
            ShippingInvoice invoice = invoiceFacade.find(this.invoiceNumber);
            if (invoice != null) {
                this.getSelected().getInvoices().add(invoice);
            }
        }
    }

    public String prepareCreate() {
        this.current = new Shipment();
        return "pretty:prepareManifesto";
    }

    public String create() {
        try {
            getFacade().create(current);
            JsfUtil.addSuccessMessage("Shipping Manifesto Succesfully Created");
            return "pretty:prepareManifesto";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, bundle.getString("PersistenceErrorOccured"));
            return null;
        }
    }
    private TreeNode root;

    public TreeNode getShipments() {
        if (root == null) {
            root = constructTree(getFacade().findAll());
        }
        return root;
    }

    private TreeNode constructTree(List<Shipment> items) {
        TreeNode rootNode = new DefaultTreeNode("root", null);
        for (Shipment shipment : items) {
            TreeNode shipmentNode = new DefaultTreeNode("shipment", shipment, rootNode);
            for (ShippingInvoice item : shipment.getInvoices()) {
                TreeNode itemNode = new DefaultTreeNode("item", item, shipmentNode);
            }
        }
        return rootNode;
    }
}
